package net.samsb;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;

import net.samsb.bootReceiver.BootBroadcastReceiver;
import net.samsb.log.LogUtils;
import net.samsb.request.BaseRequest;
import net.samsb.tool.MyActivityManager;
import net.samsb.tool.MyHandlerManager;
import net.samsb.tool.ReconnectionWSThread;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.WebSocket;

/**
 * Created by Destiny_hao on 2018/3/22.
 */

public class BaseApplication extends Application {

    public static OkHttpClient mOkHttpClient;
    public static MyHandlerManager mHandler;

    public static boolean isFirstOpen = true;
    public static boolean isFirstConnect = true;
    public static boolean isReconnect = false; //错误重连
    public static boolean isErrorReconnect = true;
    public static boolean isNotNetWork = false;

    public static long BaseCookiesTimeS = System.currentTimeMillis();

    public static BaseRequest BaseRequestInstance;
    public static WebSocket BaseWebSocket;
    public static SharedPreferences mSharedPreferences;
    public static SharedPreferences.Editor mEditor;
    public static ReconnectionWSThread BaseReconnectionWSThread;

    public static BootBroadcastReceiver bootBroadcastReceiver;

    private IntentFilter intentFilter;

    @Override
    public void onCreate() {

        //初始化日志
        LogUtils.setLogDir(String.valueOf(Environment.getExternalStorageDirectory()));
        LogUtils.setLogLevel(LogUtils.LogLevel.WARN);

        mHandler = new MyHandlerManager();
        BaseRequestInstance = new BaseRequest();

        BaseReconnectionWSThread = new ReconnectionWSThread();

        //init okHttp
        mOkHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .build();

        //注册网络监听
        intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        bootBroadcastReceiver = new BootBroadcastReceiver();
        registerReceiver(bootBroadcastReceiver, intentFilter);

        mSharedPreferences = getSharedPreferences("sh_setting", Context.MODE_PRIVATE);
        mEditor = mSharedPreferences.edit();

        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                MyActivityManager.getInstance().setCurrentActivity(activity);
            }

            @Override
            public void onActivityStarted(Activity activity) {
                //MyActivityManager.getInstance().setCurrentActivity(activity);
            }

            @Override
            public void onActivityResumed(Activity activity) {
                MyActivityManager.getInstance().setCurrentActivity(activity);

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });

        //AutoLayoutConifg.getInstance().useDeviceSize();
        super.onCreate();

    }

    public static BaseRequest getBaseRequestInstance() {

        return BaseRequestInstance;
    }
}
